package com.focus.wecenter.pojo;

import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.Type;

import javax.persistence.*;
import java.util.Date;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
 * @description
 * TODO:这里报user_question表不存在，应该是多对多关系没处理好，但是还没有写多对多关系啊？
 * @author		Lv Jing
 * @date		2018-1-6 11:05:58
 */
@Entity
@Table(name = "question", schema = "wecenter")
@DynamicUpdate
@DynamicInsert
public class Question implements Serializable {
	private static final long serialVersionUID = 1L;

	private Integer id;
	/**标题*/
	private String title;
	/**问题详细描述内容，这里后期可以考虑另外创建一个表保存内容，防止查询时效率有影响*/
	private String content;
	/**分类名称*/
	private String category;
	/**悬赏积分*/
	private Integer rewardIntegral;
	/**问题关注数*/
	private Integer concernNumber;
	/**评论数*/
	private Integer commentNumber;
	/**回答计数*/
	private Integer answerCount;
	/**创建时间*/
	private Date createTime;
	/**修改时间*/
	private Date updateTime;
    /**创建用户*/
    private User user;
    /**所属话题，可以在多个话题下*/
    private Set<Topic> involvedTopics = new HashSet<>(0);
	/**该问题的回答*/
    private Set<Answer> answers = new HashSet<>(0);
    /**关注该问题的用户*/
    private Set<User> focusUsers = new HashSet<>(0);

    @JoinTable(name = "question_focus", joinColumns = {@JoinColumn(name = "question_id")}, inverseJoinColumns = {@JoinColumn(name = "user_id")})
    @ManyToMany
    public Set<User> getFocusUsers() {
        return focusUsers;
    }
    public void setFocusUsers(Set<User> focusUsers) {
        this.focusUsers = focusUsers;
    }
    @JoinColumn(name = "question_id")
    @OneToMany
    @OrderBy("createTime asc")
	public Set<Answer> getAnswers() {
		return answers;
	}
	public void setAnswers(Set<Answer> answers) {
		this.answers = answers;
	}
	/**
     * mappedBy对应hibernate中的inverse属性，TODO：但是在这里报不能与JoinTable同时使用异常
     */
    @JoinTable(name = "topic_question", joinColumns = {@JoinColumn(name = "question_id")}, inverseJoinColumns = {@JoinColumn(name = "topic_id")})
    @ManyToMany
    @OrderBy("discussCount desc")
    public Set<Topic> getInvolvedTopics() {
        return involvedTopics;
    }
    public void setInvolvedTopics(Set<Topic> involvedTopics) {
        this.involvedTopics = involvedTopics;
    }
    @Id
    @Column(name = "QUESTION_ID")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
    /**
     * 注解实现多对一，用ManyToOne，然后用JoinColumn指定外键
     */
	@JoinColumn(name = "USER_ID")
    @ManyToOne
    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }
	@Basic
    @Column(name = "TITLE")
	public String getTitle() {
		return this.title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
    /**
     * hibernate映射Text类型使用注解Lob和Type(type = "text")
     */
	@Basic
    @Lob
    @Type(type = "text")
    @Column(name = "CONTENT")
	public String getContent() {
		return this.content;
	}
	public void setContent(String content) {
		this.content = content;
	}	
	@Basic
    @Column(name = "REWARD_INTEGRAL")
	public Integer getRewardIntegral() {
		return this.rewardIntegral;
	}
	public void setRewardIntegral(Integer rewardIntegral) {
		this.rewardIntegral = rewardIntegral;
	}
    @Basic
    @Column(name = "CATEGORY")
    public String getCategory() {
        return category;
    }
    public void setCategory(String category) {
        this.category = category;
    }
    @Basic
    @Column(name = "CONCERN_NUMBER")
	public Integer getConcernNumber() {
		return this.concernNumber;
	}
	public void setConcernNumber(Integer concernNumber) {
		this.concernNumber = concernNumber;
	}	
	@Basic
    @Column(name = "COMMENT_NUMBER")
	public Integer getCommentNumber() {
		return this.commentNumber;
	}
	public void setCommentNumber(Integer commentNumber) {
		this.commentNumber = commentNumber;
	}	
	@Basic
    @Column(name = "ANSWER_COUNT")
	public Integer getAnswerCount() {
		return this.answerCount;
	}
	public void setAnswerCount(Integer answerCount) {
		this.answerCount = answerCount;
	}	
	@Basic
    @Column(name = "CREATE_TIME")
	public Date getCreateTime() {
		return this.createTime;
	}
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}	
	@Basic
    @Column(name = "UPDATE_TIME")
	public Date getUpdateTime() {
		return this.updateTime;
	}
	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()){
            return false;
        }
        Question question = (Question) o;
        return Objects.equals(id, question.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }
}